package com.librarydemo.demo.dao;

import com.librarydemo.demo.entity.Book;
import com.librarydemo.demo.entity.BorrowRecord;
import com.librarydemo.demo.entity.User;
import com.librarydemo.demo.util.StringUtil;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserDao {


    /**登录验证，登陆成功返回User
     *登陆失败，返回null
     * @param username
     * @param password
     * @return
     */
    @Select("select * from tb_user where username= #{username} and password= #{password}")
    User loginCheck(@Param("username") String username, @Param("password") String password) ;

    /**检查用户是否已经存在，若已存在，返回>0
     * 若不存在，返回0
     * @param username
     * @return
     */
    @Select("select count(*) from tb_user where username= #{username}")
    int nameCheck(@Param("username") String username) ;

    /**用户注册
     *注册成功，返回1
     * @param username
     * @param password
     * @param borrowNumber
     * @param phoneNumber
     * @param picture
     * @return
     */
    @Insert("insert into tb_user(username, password, borrowNumber, phoneNumber, picture) values( #{username}, #{password}, #{borrowNumber}, #{phoneNumber}, #{picture}) ")
    int register(@Param("username") String username, @Param("password") String password, @Param("borrowNumber") int borrowNumber, @Param("phoneNumber") int phoneNumber, @Param("picture") String picture) ;

    /**用户更新头像
     * 更新成功，返回1
     * @param uid
     * @param picture
     * @return
     */
    @Update("update tb_user set picture= #{picture} where uid= #{uid}")
    int updatePic(@Param("uid") int uid, @Param("picture") String picture) ;

    /**获取用户头像
     *
     * @param uid
     * @return
     */
    @Select("select picture from tb_user where uid= #{uid}")
    String getPicture(@Param("uid") int uid) ;

    /**用户更新密码
     *
     * @param uid
     * @param password
     * @return
     */
    @Update("update tb_user set password= #{password} where uid= #{uid}")
    int updatePassword(@Param("uid") int uid, @Param("password") String password) ;

    /**根据id获取用户信息
     *
     */
    @Select("select * from tb_user where uid= #{uid}")
    User getById(@Param("uid") int uid);

    /**判断用户现在是否可以借书，大于0才可以借书
     *
     */
    @Select("select borrowNumber from tb_user where uid= #{uid}")
    int checkUserBorrow(@Param("uid") int uid) ;

    /**借阅图书
     *如若可以借书，则借书，借书需要判断remain_book的数量，若大于0才可以借书。借书成功，需要修改borrowNumber的值，同时增加一条借书的记录
     * 判断现在是否有图书可借，remain_num > 0 才可以借
     */
    @Select("select * from tb_book where bid= #{bid}")
    Book checkBookBorrow(@Param("bid") int bid) ;

    /**借阅图书,借阅成功，remain_num的数量减1
     *
     */
    @Update("update tb_book set remain_num= remain_num - 1 where bid= #{bid}")
    int updateBookRemain_num(@Param("bid") int bid) ;

    /**借书成功，修改borrowNumber的值
     *
     */
    @Update("update tb_user set borrowNumber= borrowNumber - 1 where uid= #{uid}")
    int updateBorrowNumber(@Param("uid") int uid) ;

    /**借书成功，增加一条借书记录
     *
     */
    @Insert("insert into tb_borrowRecord(uid, bid, borrowDate, bname, author, publisher, returnDate) values(#{uid}, #{bid}, #{borrowDate}, #{bname}, #{author}, #{publisher}, #{returnDate})")
    int addBorrowRecord(@Param("uid") int uid, @Param("bid") int bid, @Param("borrowDate") String borrowDate, @Param("bname") String bname, @Param("author") String author, @Param("publisher") String publisher, @Param("returnDate") String returnDate) ;

    /**还书
     *增加一条还书记录，remain_num+1, 修改borrowNumber的值+1,删除对应的借书记录
     */
    //查询当前的归还书籍是否过期，即返回借阅信息后做判断
    @Select("select * from tb_borrowRecord where uid= #{uid} and bid= #{bid}")
    BorrowRecord getBorrowRecord(@Param("uid") int uid, @Param("bid") int bid);

    @Delete("delete from tb_borrowRecord where uid= #{uid} and bid= #{bid}")
    int deleteBorrowRecord(@Param("uid") int uid, @Param("bid") int bid);

    @Update("update tb_book set remain_num= remain_num + 1 where bid= #{bid}")
    int addOneBookRemain_num(@Param("bid") int bid);

    @Update("update tb_user set borrowNumber = borrowNumber + 1 where uid= #{uid}")
    int addOneBorrowNumber(@Param("uid") int uid);

    @Insert("insert into tb_returnRecord(uid, bid, bname, author, publisher, returnDate) values(#{uid}, #{bid}, #{bname}, #{author}, #{publisher}, #{returnDate})")
    int addReturnRecord(@Param("uid") int uid, @Param("bid") int bid, @Param("bname") String bname, @Param("author") String author, @Param("publisher") String publisher, @Param("returnDate") String returnDate);

    //查询所有的借阅记录
    @Select("select * from tb_borrowRecord where uid= #{uid}")
    List<BorrowRecord> getAllBorrowRecord(int uid);


    //多条件搜索书籍
    List<Book> getBooks(@Param("nameKey") String name, @Param("author") String author, @Param("publisher") String publisher, @Param("catalog") String catalog);
}
